<?php
namespace Kuoqi\Cli;

use Kuoqi\Cli\Compress\Compressjs;

class Compress extends Behavior {
    protected $document = array(
        "[compress js -i filename -o filename -type path]压缩js文件并保存到指定位置",
        "-i filename 为输入文件的路径",
        "-o filename 为输出文件的路径",
        "-type [r/a/c] [relative/absolutely/complete]为输出文件的路径，r,为相对当前展开文件的位置，a,为执行程序主文件的位置，c为输入输出的完整路径信息,输入与输出的文件的type应保持一致，默认使用r",
        "注意两个反斜杠的书写要使用转义，否则压缩过程中会错误匹配为注释信息导致文本压缩异常"
        );
    /**
     * createby kuoqi at 2022/11/11 23:21 初始化start的方法
     * //php kuoqi.php compress js -i kuoqi-php.v1.00.03/application/test.js -o kuoqi-php.v1.00.03/application/test.min.js
     */
    public function start(){
        if($this->command=='js'){
            if(!isset($this->params['-i'])){ $this->error("请输入-i参数");}
            if(!isset($this->params['-o'])){ $this->error("请输入-o参数");}
            if(!isset($this->params['-type'])){ $this->params['-type'] = 'r';}
            $fileDriver = new \Kuoqi\FileDriver('/'.$this->params['-i']);
            if(!$fileDriver->exist()){
                $this->error("输入文件不存在，请检查");
            }
            $compress = new Compress\Compressjs();
            $compress->input($fileDriver->getContent());
            $fileoutDriver = new \Kuoqi\FileDriver('/'.$this->params['-o']);
            $fileoutDriver->setContent($compress->output());
            $fileoutDriver->safe_code = "";
            if(!$fileoutDriver->save()){
                $this->error('文件保存失败:'.$fileoutDriver->getError());
            }
            $this->error('ok');
        }else{
            $this->help();//展示帮助方法
        }
    }
}